package org.example.controller;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.databind.util.JSONPObject;
import org.example.common.exception.CustomException;
import org.example.common.response.ResponseData;
import org.example.common.response.ResponseEnum;
import org.example.entity.Car;
import org.example.entity.param.UserParam;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.Map;

// @CrossOrigin注解:允许被前端跨域请求
@CrossOrigin
// @RequestMapping("/api")注解:当前控制器类的地址是 "/api"
// 类地址存在的意义是可以作为方法地址的命名空间
// 不允许存在两个"/save"地址, 但允许存在"/car/save"和"/record/save"
@RequestMapping("/api")
// @RestController注解: 让当前类称为Web控制器类
@RestController
public class ApiController {
    // @GetMapping("/01")注解:当前方法的地址是"/01"
    // 如果当前方法所在的类没有地址,则方法地址就是IP:端口/方法地址
    // 如果当前方法所在的类有地址,则方法地址是IP:端口/类地址/方法地址
    @GetMapping("/01")
    public String m01() {
        return "兰博基尼";
    }

    @GetMapping("/02")
    public String m02() {
        return "玛莎拉蒂";
    }

    @GetMapping("/03/{brand}/{type}")
    public String m03(@PathVariable("brand") String brand,
                      @PathVariable("type") String type) {
        return brand + ":" + type;
    }
    @PostMapping("/04")
    public String m04(String username,String password) {
        return username + ":" + password;
    }

    @GetMapping("/05")
    public String m05(HttpServletRequest request){
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        return username + ":" + password;
    }
    @GetMapping("/06")
    public String m06(UserParam param) {
        return param.getUsername() + ":" + param.getPassword();
    }
    @GetMapping("/07")
    public void m07(HttpServletResponse response) throws IOException {
        // 这个car是需要返回给前端的对象
        Car car = new Car(8,"兰博基尼","兰博基尼",18.5,0);
        // 设置字符集
        response.setCharacterEncoding("utf-8");
        // 设置内容类型为json格式
        response.setContentType("application/json;charset=utf-8");
        // 获取输出流
        PrintWriter out = response.getWriter();
        // 用JSON工具类把对象转为json字符串
        String json = JSON.toJSONString(car);
        // 用输出流输出这个json字符串
        out.write(json);
        // 冲刷输出流
        out.flush();
        // 关闭输出流
        out.close();
    }

    @GetMapping("/08")
    public Car m08() {
        // 这个car是需要返回给前端的对象
        return new Car(8, "兰博基尼", "兰博基尼", 18.5, 0);
    }

    @GetMapping("/09")
    public ResponseData m09() {
        return ResponseData.success();
    }
    @GetMapping("/10")
    public ResponseData m10() {
        Car car = new Car(8, "兰博基尼", "兰博基尼", 18.5, 0);
        return ResponseData.success(car);
    }
    @GetMapping("/11")
    public ResponseData m11() throws SQLException {
        if ( 3 > 2 ) {
            throw new SQLException();
        }
        Car car = new Car(8, "兰博基尼", "兰博基尼", 18.5, 0);
        return ResponseData.success(car);
    }
    @GetMapping("/12")
    public ResponseData m12() {
        if ( 3 > 2 ) {
            throw new CustomException(ResponseEnum.USER_NOT_EXIST);
        }
        Car car = new Car(8, "兰博基尼", "兰博基尼", 18.5, 0);
        return ResponseData.success(car);
    }
    @GetMapping("/13")
    public ResponseData m13() {
        int x = 10 / 0;
        Car car = new Car(8, "兰博基尼", "兰博基尼", 18.5, 0);
        return ResponseData.success(car);
    }
}
